package com.uspacex.calculate.service;

import com.uspacex.calculate.entity.TaskDetail;
import com.uspacex.calculate.mapper.taskCalculateMapper;
import com.singularsys.jep.Jep;
import com.singularsys.jep.JepException;
import com.singularsys.jep.ParseException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Component
public class CalculateTool {
    TaskDetail taskDetail;

    @Autowired
    com.uspacex.calculate.mapper.taskDetailMapper taskDetailMapper;

    @Autowired
    taskCalculateMapper taskCalculateMapper;

    public static CalculateTool calculateTool;

    @PostConstruct
    public void init() {
        calculateTool = this;
    }

    int id;
    String taskId = "";
    String taskName = "";
    String taskDescription = "";
    String PRI_Name = "";
    String PRI_Type = "";
    Date timeCreate;
    Date timeCompleted;
    String databaseName = "";
    String tableName = "";
    String taskTableName = "";
    String taskGlobalTableName = "";
    Double taskProcessRate;
    int sum;
    boolean first_global = true;

    // MARK LinkedHashMap
    LinkedHashMap<String, Object> tableMap = new LinkedHashMap<>();

    Map<String, Object> tableGlobalMap = new HashMap<>();
    Set<String> originset = new HashSet<String>();
    Set<String> tag_set = new HashSet<>();
    Map<String, Double> originMap = new HashMap<>();
    ArrayList<String> formulas = new ArrayList<>();
    Map<String, String> conditions = new HashMap<>();
    private static final Logger logger = LoggerFactory.getLogger(CalculateTool.class);

    // MARK
    List<LinkedHashMap<String, Object>> batchList = new ArrayList<>();


    public void calculateAll() throws IOException, JepException {
        long startTime = System.nanoTime();
        Map name = new HashMap();
        name.put("taskName", tableName);
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        Properties properties = new Properties();
        properties.setProperty("jdbc.driver", "com.mysql.cj.jdbc.Driver");
        properties.setProperty(
                "jdbc.url",
                "jdbc:mysql://localhost:3306/" + databaseName + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
        properties.setProperty("jdbc.username", "root");
        properties.setProperty("jdbc.password", "root");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
        SqlSession session = sqlSessionFactory.openSession();

        // MARK 2 解析公式，计算全局变量
        //parseFormulas();
        //calculateGlobal();
        parseRules();

        try {
            session.select("com.uspacex.calculate.mapper.taskCalculateMapper.selectAll", tableName, new ResultHandler() {
                        Double count = 0.00;

                        public void handleResult(ResultContext resultContext) {
                            Map<String, Object> map = (Map<String, Object>) resultContext.getResultObject();
                            count = count + 1.00;
                            for (Map.Entry<String, Object> entry : map.entrySet()) {
                                if (originMap.keySet().contains(entry.getKey())) {
                                    if (!entry.getValue().toString().isEmpty()) {
                                        originMap.put(entry.getKey(), Double.parseDouble(entry.getValue().toString()));
                                    } else {
                                        originMap.put(entry.getKey(), 0.00);
                                    }
                                }
                            }
                            try {
                                calculateOne();
                                if(conditions.size()>0) {
                                    judge();
                                }

                                // MARK 如果list中已经存了500条数据，就批量插入一次数据库，否则继续执行计算
                                if (!batchList.isEmpty() && batchList.size() > 500) {
                                    calculateTool.taskCalculateMapper.batchInsert(taskTableName, batchList, PRI_Name, tableMap, tag_set);
                                    batchList = new ArrayList<>();
                                }

                                //count=count+1.00;
                                taskProcessRate = count / sum;
                                if (count % 1000 == 0) {
                                    calculateTool.taskDetailMapper.save_process(taskProcessRate, taskId);
                                }
                                if (count == sum) {


                                    // MARK 如果计算完成，list中还有一部分数据，将最后这部分数据插入数据库
                                    calculateTool.taskCalculateMapper.batchInsert(taskTableName, batchList, PRI_Name, tableMap, tag_set);


                                    calculateTool.taskDetailMapper.save_process(taskProcessRate, taskId);
                                    Date date = new Date();
                                    calculateTool.taskDetailMapper.completeTime(date, taskId);
                                    calculateTool.taskDetailMapper.finish(taskId);
                                }
                            } catch (JepException | IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }
            );

        } finally {
            session.close();
        }


        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("CalculateAll程序运行时间： " + (endTime - startTime) / 1000000 + "ns");

    }
//    public String getPRI_Name(String ){
//
//    }

    /**
     * 规则判断
     *
     * @throws JepException
     */
    public void judge() throws JepException {
        //对规则中每条变量注入值并判断
        long startTime = System.nanoTime();
        for (Map<String, Object> map : parsedRules) {
            boolean flag = setFlag((Jep) map.get("jep"));

            String os = conditions.get((String) map.get("ruleString"));
            String[] nameStrArray = os.split(",");
            for (String tag_str : nameStrArray) {
                String[] tag_list = tag_str.split("=");
                String tag_name = tag_list[0];
                String tag_value = null;
                if (flag) {
                    tag_value = tag_list[1];
                }
                batchList.get(batchList.size() - 1).putIfAbsent(tag_name, tag_value);
            }
        }
        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("judge程序运行时间： " + (endTime - startTime) / 1000000 + "ns");
        System.out.println("batchList.get(batchList.size() - 1): " + batchList.get(batchList.size() - 1));
    }


    public boolean setFlag(Jep jep) throws JepException {

        long startTime = System.nanoTime();


        String r = analysis(jep);
        String[] fva = r.split(", ");

        for (int j = 0; j < fva.length; j++) {
            boolean find = false;

            for (String s : originMap.keySet()) {

                if (fva[j].trim().equals(s.trim())) {
                    Double value = originMap.get(s);
                    jep.addVariable(s.trim(), value);

                    find = true;
                    break;
                }
            }
            if (!find) {
                for (String s : tableMap.keySet()) {
                    System.out.println("1111" + fva[j].trim());
                    System.out.println("2222" + s.trim());
                    System.out.println("3333" + fva[j].trim().equals(s.trim()));
                    if (fva[j].trim().equals(s.trim())) {
                        Double value = (Double) tableMap.get(s);
                        System.out.println("jeptable:" + s + "放入" + value);
                        jep.addVariable(s.trim(), value);

                        find = true;
                        break;
                    }
                }
            }
            if (!find) {
                for (String s : tableGlobalMap.keySet()) {

                    if (fva[j].trim().equals(s.trim())) {
                        jep.addVariable(s.trim(), tableGlobalMap.get(s));

                        break;
                    }
                }
            }
        }
        boolean flag = (Boolean) jep.evaluate();
        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("setFlag运行时间： " + (endTime - startTime) / 1000000 + "ns");
        return flag;
    }

    List<Map<String, Object>> parsedNormalFormulas = new ArrayList<>();
    List<Map<String, Object>> parsedGlobalFormulas = new ArrayList<>();
    List<Map<String, Object>> parsedRules = new ArrayList<>();

    public Jep jepParseString(String str) throws ParseException {
        Jep jep = new Jep(); //一个数学表达式
        jep.setAllowUndeclared(true);
        jep.getVariableTable().remove("e");
        jep.getVariableTable().remove("true");
        jep.getVariableTable().remove("false");
        jep.getVariableTable().remove("pi");
        jep.getVariableTable().remove("i");
        jep.parse(str);
        return jep;
    }

    public void parseRules() throws JepException, IOException {
        for (String s : conditions.keySet()) {
            Map<String, Object> map = new HashMap<>();
            Jep jep = new Jep(); //一个数学表达式
            jep.setAllowUndeclared(true);
            jep.parse(s.trim());
            map.put("jep", jep);
            map.put("ruleString", s);
            parsedRules.add(map);
        }

    }

    public void parseFormulas() throws JepException, IOException {
        //对记录逐一运用公式
        for (int i = 0; i < formulas.size(); i++) {
            String input = formulas.get(i);
            String[] nameStrArray = input.split("=");
            String p = nameStrArray[0];
            String formula = "";
            if (nameStrArray.length > 1) {
                formula = nameStrArray[1];
            }

            Jep jep = jepParseString(formula);

            Map<String, Object> map = new HashMap<>();
            map.put("formula", formula);
            map.put("varName", p);
            map.put("jep", jep);

            System.out.println("公式为" + formula);
            boolean isGlobal = isGlobal(formula, jep);
            //判断是否是全局变量
            if (isGlobal) {
                parsedGlobalFormulas.add(map);
            } else {
                parsedNormalFormulas.add(map);
            }
        }
    }

    public void calculateGlobal() throws JepException, IOException {
        for (Map<String, Object> map : parsedGlobalFormulas) {
            String formula = (String) map.get("formula");
            String p = (String) map.get("varName");
            Jep jep = (Jep) map.get("jep");

            if (formula.contains("MAX") || formula.contains("MIN") || formula.contains("SUM")
                    || formula.contains("AVG") || formula.contains("COUNT")) {
                Double global_variable = cal_variable(formula);
                System.out.println("全局变量" + p + ":" + global_variable + " formula" + formula);
                tableGlobalMap.put(p, global_variable);
            } else {
                String re_ana = analysis(jep);
                String[] vaArray = re_ana.split(", ");
                for (int j = 0; j < vaArray.length; j++) {
                    for (String s : tableGlobalMap.keySet()) {
                        if (vaArray[j].equals(s)) {
                            jep.addVariable(s, tableGlobalMap.get(s));
                            //System.out.println("jep放了"+s+":"+tableGlobalMap.get(s));
                        }
                    }
                }
                Double result = (Double) jep.evaluate();
                tableGlobalMap.put(p, result);
            }
        }
    }

    public void calculateOne() throws JepException, IOException {
        System.out.println("单条记录开始计算");
        long startTime = System.nanoTime();

        // MARK added
        LinkedHashMap<String, Object> valueMap = new LinkedHashMap<>();
        // MARK 将主键和键值放入map
        String k = PRI_Name;
        valueMap.put(k, originMap.get(PRI_Name).intValue());

        //对记录逐一运用公式
        for (Map<String, Object> map : parsedNormalFormulas) {
            String p = (String) map.get("varName");
            Jep jep = (Jep) map.get("jep");

            try {
                String v = jep.getVariableTable().keySet().toString();
                String v2 = v.substring(1, v.length() - 1);
                String[] vaArray = v2.split(", ");
                for (int j = 0; j < vaArray.length; j++) {
                    String s = vaArray[j];
                    if (inorigin(s)) {
                        jep.addVariable(s, originMap.get(s));

                    } else if (insingle(s)) {
                        jep.addVariable(s, tableMap.get(s));

                    } else if (inquan(s)) {
                        jep.addVariable(s, (Double) tableGlobalMap.get(s));

                    }
                }
                Double result = (Double) jep.evaluate();

                tableMap.put(p, result);

                // MARK added
                valueMap.put(p, result);

            } catch (JepException e) {
                e.printStackTrace();
            }

        }
        //把中间变量持久化
        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("单条记录完成计算时间： " + (endTime - startTime) / 1000000 + "ns");

        // MARK 暂不更新数据库，先放入list。保存全局变量
        // save();
        batchList.add(valueMap);
        if (first_global) {
            calculateTool.taskCalculateMapper.saveTable(taskGlobalTableName, PRI_Name, id, tableGlobalMap);
            first_global = false;
        }


    }

/* MARK 去掉
    public void save() throws IOException {
        long startTime=System.nanoTime();
        System.out.println("编号"+originMap.get(PRI_Name)+"单次变量如下：");
        for (String key : tableMap.keySet()) {
            System.out.println("Key: " + key + " Value: " + tableMap.get(key));
        }

        calculateTool.taskCalculateMapper.saveTable(taskTableName,PRI_Name,originMap.get(PRI_Name).intValue(),tableMap);
        System.out.println("全局变量如下：");
        for (String key : tableGlobalMap.keySet()) {
            System.out.println("Key: " + key + " Value: " + tableGlobalMap.get(key));
        }

        System.out.println("-----------------------");
        //第一次存全局变量，之后不存
        if (first_global) {
            calculateTool.taskCalculateMapper.saveTable(taskGlobalTableName,PRI_Name,id,tableGlobalMap);
            first_global=false;
        }
        long endTime=System.nanoTime(); //获取结束时间
        System.out.println("单条记录save运行时间： "+(endTime-startTime)/1000000+"s");
    }
*/


    private Double cal_variable(String formula) throws IOException {
        long startTime = System.nanoTime();
        Double result;
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        Properties properties = new Properties();
        properties.setProperty("jdbc.driver", "com.mysql.cj.jdbc.Driver");
        properties.setProperty(
                "jdbc.url",
                "jdbc:mysql://localhost:3306/" + databaseName + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
        properties.setProperty("jdbc.username", "root");
        properties.setProperty("jdbc.password", "root");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
        SqlSession session = sqlSessionFactory.openSession();
        try {
            taskCalculateMapper mapper = session.getMapper(taskCalculateMapper.class);
            result = mapper.getGlobal(tableName, formula);
        } finally {
            session.close();
        }
        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("cal_variable计算全局变量运行时间： " + (endTime - startTime) / 1000000 + "s");
        return result;
    }


    /**
     * 判断是否是全局变量，如：MAX(a),两个全局变量的四则运算，常数
     *
     * @param formula
     * @return
     * @throws ParseException
     */
    public boolean isGlobal(String formula, Jep jep) throws ParseException {
        if (formula.equals("")) {
            return false;
        } else {
            boolean find = false;
            int length = jep.getVariableTable().keySet().size();
            if (length == 0) {
                find = true;
            } else if (formula.contains("MAX") || formula.contains("MIN")
                    || formula.contains("SUM") || formula.contains("AVG")
                    || formula.contains("COUNT")) {
                find = true;
            } else {
                String v = jep.getVariableTable().keySet().toString();
                String v2 = v.substring(1, v.length() - 1);
                String[] vaArray = v2.split(", ");
                for (int i = 0; i < vaArray.length; i++) {
                    if (inquan(vaArray[i])) {
                        find = true;
                    } else {
                        find = false;
                        break;
                    }
                }
            }
            return find;
        }
    }

    /**
     * 判断是否在单次记录map中
     *
     * @param ss
     * @return
     */
    public boolean insingle(String ss) {
        boolean find = false;
        for (String s : tableMap.keySet()) {
            if (ss.equals(s)) {
                find = true;
            }
        }
        return find;
    }

    /**
     * 判断是否在全局map中
     *
     * @param ss
     * @return
     */
    public boolean inquan(String ss) {
        boolean find = false;
        for (String s : tableGlobalMap.keySet()) {
            if (ss.equals(s)) {
                find = true;
            }
        }
        return find;
    }

    /**
     * 判断是否在原来数据中
     *
     * @param ss
     * @return
     */
    public boolean inorigin(String ss) {

        boolean find = false;
        for (String s : originMap.keySet()) {
            if (ss.equals(s)) {
                find = true;
            }
        }
        return find;

    }


    public void config(Map<String, Object> config, TaskDetail taskDetail) throws IOException, ParseException {
        long startTime = System.nanoTime();
        id = taskDetail.getId();
        taskId = taskDetail.getTaskId();
        taskProcessRate = 0.00;
        taskTableName = taskDetail.getTaskTableName();
        taskGlobalTableName = taskDetail.getTaskGlobalTableName();
        tableName = taskDetail.getTableName();
        taskName = taskDetail.getTaskName();
        taskDescription = taskDetail.getTaskDescription();
        databaseName = taskDetail.getDatabaseName();
        System.out.println("测试数据库" + tableName + databaseName);
        Iterator<Map.Entry<String, Object>> entries = config.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            if (entry.getKey().equals("formula")) {
//                for (String formula : (ArrayList<String>) entry.getValue()) {
//                    formulas.add(formula);
//                }
                String formulas_str = (String) entry.getValue();
                String[] formulass = formulas_str.replace(" ", "").split("\n");
                for (int i = 0; i < formulass.length; i++) {
                    //System.out.println("you"+formulass[i]);
                    //System.out.println("size0"+formulas.size());
                    if (!formulass[i].trim().equals("")) {
                        formulas.add(formulass[i]);
                    }
                }
                //System.out.println("size1"+formulas.size());
            } else if (entry.getKey().equals("rules")) {
//                Map<String, String> rule = (Map<String, String>) entry.getValue();
//                for (Map.Entry<String, String> r_entry : rule.entrySet()) {
//                    conditions.put(r_entry.getKey(), r_entry.getValue());
//                }
                String rules_str = (String) entry.getValue();
                if(rules_str.length()>0) {
                    String[] ruless = rules_str.split("\n");
                    for (int j = 0; j < ruless.length; j++) {
                        String rule = ruless[j];
                        String[] rule_s = rule.split(":");
                        conditions.put(rule_s[0].replace(" ", ""), rule_s[1]);
                    }
                }
            }
        }
        sum = returnSum(databaseName, tableName);
        origin_set(databaseName, tableName);
        timeCreate = new Date();
        /*
        for(String s:formulas){
            System.out.println("公式有："+s);
        }
        for(String s:conditions.keySet()){
            System.out.println("条件有"+s+"操作"+conditions.get(s));
        }
        System.out.println("size:"+formulas.size());
        */

        /*for (int i = 0; i < formulas.size(); i++) {
            String input = formulas.get(i);
            String[] nameStrArray = input.split("=");
            String formula = "";
            String p = nameStrArray[0];
            if (nameStrArray.length > 1) {
                formula = nameStrArray[1];
            }
            if (isGlobal(formula, jepParseString(formula))) {
                //全局变量
                tableGlobalMap.put(p, 0.00);
            } else {
                //单行变量
                tableMap.put(p, 0.00);
            }
            //解析公式，把需要的原生的变量存入originMap
            String re = analysis(jepParseString(formula));
//            for(String ss:originset){
//                System.out.println("需要的originset:"+ss);
//            }
            String[] vaArray = re.split(", ");
            for (int q = 0; q < vaArray.length; q++) {
                for (String s : originset) {
                    if (vaArray[q].equals(s)) {
                        originMap.put(s, 0.00);
                    }
                }
            }
        }*/


        //解析规则,把需要的变量标注
        for (int j = 0; j < conditions.keySet().size(); j++) {
            String tag_need = analysis(jepParseString((String) conditions.keySet().toArray()[j]));
            System.out.println("tag_need:" + tag_need);
            String[] vaArray = tag_need.split(", ");
            for (int q = 0; q < vaArray.length; q++) {
                for (String s : originset) {
                    if (vaArray[q].equals(s)) {
                        originMap.put(s, 0.00);
                    }
                }
            }
        }


        System.out.println("单行变量有");
        System.out.println(tableMap.keySet());
        System.out.println("全局变量有");
        System.out.println(tableGlobalMap.keySet());
        System.out.println("需要的变量有");
        System.out.println(originMap.keySet());

        for (String s : conditions.keySet()) {
            String os = conditions.get(s);
            //System.out.println("os"+os);

            if (os.contains(",")) {
                String[] nameStrArray = os.split(",");
                for (int i = 0; i < nameStrArray.length; i++) {
                    String[] tagName = nameStrArray[i].split("=");
                    tag_set.add(tagName[0]);
                }

            } else {
                String[] tagName1 = os.split("=");
                tag_set.add(tagName1[0]);
            }

        }
        System.out.println("标签有" + tag_set.toString());
        //创建两表
        List<String> list_table = new ArrayList<>();
        for (String key : tableMap.keySet()) {
            list_table.add(key);
        }
        System.out.println("PRICESHI"+PRI_Name+PRI_Type);
        calculateTool.taskCalculateMapper.createTable(taskTableName, PRI_Name, PRI_Type, list_table, tag_set);

        List<String> list_global = new ArrayList<>();
        for (String key : tableGlobalMap.keySet()) {
            list_global.add(key);
        }
        Set<String> tag_global = new HashSet<>();
        calculateTool.taskCalculateMapper.createTable(taskGlobalTableName, PRI_Name, "int", list_global, tag_global);

        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("config程序运行时间： " + (endTime - startTime) / 1000000 + "ns");

    }

    public List<Map> origin_set(String databaseName, String tableName) throws IOException {
        long startTime = System.nanoTime();
        List<Map> map;
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        Properties properties = new Properties();
        properties.setProperty("jdbc.driver", "com.mysql.cj.jdbc.Driver");
        properties.setProperty(
                "jdbc.url",
                "jdbc:mysql://localhost:3306/" + databaseName + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
        properties.setProperty("jdbc.username", "root");
        properties.setProperty("jdbc.password", "root");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
        SqlSession session = sqlSessionFactory.openSession();

        try {
            taskCalculateMapper mapper = session.getMapper(taskCalculateMapper.class);
            map = mapper.origin_set2(tableName);
        } finally {
            session.close();
        }
        for (Map<String, Object> kv : map) {
            String COLUMN_NAME = String.valueOf(kv.get("COLUMN_NAME"));
            if (String.valueOf(kv.get("COLUMN_KEY")).equals("PRI")) {
                originMap.put(COLUMN_NAME, 0.00);
                PRI_Name = COLUMN_NAME;
                PRI_Type = String.valueOf(kv.get("COLUMN_TYPE"));
            }
            originset.add(COLUMN_NAME);
        }
        System.out.println("PRI_Name"+PRI_Name);
        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("origin_set程序运行时间： " + (endTime - startTime) / 1000000 + "ns");
        return map;
    }

    public String getPRI_Name(String databaseName, String tableName) throws IOException {
        List<Map> map;
        String key_Name = "";
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        Properties properties = new Properties();
        properties.setProperty("jdbc.driver", "com.mysql.cj.jdbc.Driver");
        properties.setProperty(
                "jdbc.url",
                "jdbc:mysql://localhost:3306/" + databaseName + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
        properties.setProperty("jdbc.username", "root");
        properties.setProperty("jdbc.password", "root");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
        SqlSession session = sqlSessionFactory.openSession();

        try {
            taskCalculateMapper mapper = session.getMapper(taskCalculateMapper.class);
            map = mapper.origin_set2(tableName);

        } finally {
            session.close();
        }
        for (Map<String, Object> kv : map) {
            String COLUMN_NAME = String.valueOf(kv.get("COLUMN_NAME"));
            if (String.valueOf(kv.get("COLUMN_KEY")).equals("PRI")) {
                key_Name = COLUMN_NAME;
                //PRI_Type=String.valueOf(kv.get("COLUMN_TYPE"));
            }

        }

        return key_Name;
    }


    public int returnSum(String databaseName, String tableName) throws IOException {
        long startTime = System.nanoTime();
        int result = 0;
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        Properties properties = new Properties();
        properties.setProperty("jdbc.driver", "com.mysql.cj.jdbc.Driver");
        properties.setProperty(
                "jdbc.url",
                "jdbc:mysql://localhost:3306/" + databaseName + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true");
        properties.setProperty("jdbc.username", "root");
        properties.setProperty("jdbc.password", "root");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
        SqlSession session = sqlSessionFactory.openSession();

        try {
            taskCalculateMapper mapper = session.getMapper(taskCalculateMapper.class);
            result = mapper.getSum2(tableName);

        } finally {
            session.close();
        }
        long endTime = System.nanoTime(); //获取结束时间
        System.out.println("returnSum程序运行时间： " + (endTime - startTime) / 1000000 + "ns");
        return result;
    }

    /**
     * jep解析
     *
     * @param jep
     * @return "a,b,c"
     */
    public String analysis(Jep jep) {
        String str = "";
        String v = jep.getVariableTable().keySet().toString();
        str = v.substring(1, v.length() - 1);
        return str;
    }


}